/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.avro.ui.syntaxcoloring;

import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.ANNOTATION;
import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.ARGUMENTS;
import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.COMMENT;
import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.DEFTYPES;
import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.ENUMFIELDS;
import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.KEYWORD;
import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.STRING;
import static de.thwildau.tm.moses.avro.ui.syntaxcoloring.AvroHighlightingConfiguration.VALUE;

import org.eclipse.xtext.impl.TerminalRuleImpl;
import org.eclipse.xtext.nodemodel.BidiIterator;
import org.eclipse.xtext.nodemodel.BidiTreeIterator;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.impl.CompositeNodeWithSemanticElement;
import org.eclipse.xtext.nodemodel.impl.HiddenLeafNode;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;

import de.thwildau.tm.moses.avro.avdl.Annotation;
import de.thwildau.tm.moses.avro.avdl.Argument;
import de.thwildau.tm.moses.avro.avdl.BooleanRef;
import de.thwildau.tm.moses.avro.avdl.DefType;
import de.thwildau.tm.moses.avro.avdl.Definitions;
import de.thwildau.tm.moses.avro.avdl.EnumFields;
import de.thwildau.tm.moses.avro.avdl.ImportTypeRef;
import de.thwildau.tm.moses.avro.avdl.Imports;
import de.thwildau.tm.moses.avro.avdl.NumberRef;
import de.thwildau.tm.moses.avro.avdl.Protocol;
import de.thwildau.tm.moses.avro.avdl.StringRef;
import de.thwildau.tm.moses.avro.avdl.Throws;
import de.thwildau.tm.moses.avro.avdl.UnionType;

/**
 * This class set the coloring for the language.
 * 
 * @author Thomas Kistel
 * @version $Revision: 1$
 * @see http://www.eclipse.org/Xtext/documentation/2_1_0/195-highlighting.php
 */
public class AvroHighlightingCalculator implements ISemanticHighlightingCalculator {

    @Override
    public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor) {
        if (resource == null || resource.getParseResult() == null)
            return;
        INode root = resource.getParseResult().getRootNode();
        BidiTreeIterator<INode> it = root.getAsTreeIterable().iterator();
        while (it.hasNext()) {
            INode node = it.next();
            // Set the coloring for annotations
            if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof Annotation) {
                setStyles(acceptor, it, ANNOTATION);
                // Set the coloring for most of keywords
            } else if (node instanceof CompositeNodeWithSemanticElement
                    && node.getSemanticElement() instanceof Definitions
                    && !(node.getSemanticElement() instanceof Annotation)) {
                setStyles(acceptor, it, KEYWORD);
            }
            // Set the coloring for Unions keyword
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof UnionType) {
                setStyles(acceptor, it, KEYWORD);
            }
            // Set the coloring for imports
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof Imports) {
                setStyles(acceptor, it, KEYWORD);
            }
            // Set the coloring for strings
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof StringRef) {
                setStyles(acceptor, it, STRING);
            }
            // Set the coloring for Throws keyword
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof Throws) {
                setStyles(acceptor, it, KEYWORD);
            }
            // Set the coloring for Import Types keyword
            else if (node instanceof CompositeNodeWithSemanticElement
                    && node.getSemanticElement() instanceof ImportTypeRef) {
                setStyles(acceptor, it, KEYWORD);
            }
            // Set the coloring for Protocol keyword
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof Protocol) {
                setStyles(acceptor, it, KEYWORD);
            }
            // Comments
            else if (node instanceof HiddenLeafNode && node.getGrammarElement() instanceof TerminalRuleImpl) {
                TerminalRuleImpl ge = (TerminalRuleImpl)node.getGrammarElement();
                if (ge.getName().equalsIgnoreCase("SL_COMMENT") || ge.getName().equalsIgnoreCase("ML_COMMENT"))
                    acceptor.addPosition(node.getOffset(), node.getLength(), COMMENT);
            }
            // Boolean value
            else if (node instanceof CompositeNodeWithSemanticElement
                    && node.getSemanticElement() instanceof BooleanRef) {
                setStyles(acceptor, it, KEYWORD);
            }
            // Numerical values
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof NumberRef) {
                setStyles(acceptor, it, VALUE);
            }
            // Arguments
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof Argument) {
                setStyles(acceptor, node.getAsTreeIterable().reverse().iterator(), ARGUMENTS);
            }
            // DefTypes
            else if (node instanceof CompositeNodeWithSemanticElement && node.getSemanticElement() instanceof DefType) {
                setStyles(acceptor, it, DEFTYPES);
            }
            // Enum fields
            else if (node instanceof CompositeNodeWithSemanticElement
                    && node.getSemanticElement() instanceof EnumFields) {
                setStyles(acceptor, it, ENUMFIELDS);
            }  
         // Enum fields
            else if (node instanceof CompositeNodeWithSemanticElement
                    && node.getSemanticElement() instanceof EnumFields) {
                setStyles(acceptor, it, ENUMFIELDS);
            }  
        }
    }

    // -------------------------------------------------
    void setStyles(IHighlightedPositionAcceptor acceptor, BidiIterator<INode> it, String... styles) {
        for (String s : styles) {
            if (!it.hasNext())
                return;
            INode n = skipWhiteSpace(acceptor, it);
            if (n != null && s != null)
                acceptor.addPosition(n.getOffset(), n.getLength(), s);
        }
    }

    INode skipWhiteSpace(IHighlightedPositionAcceptor acceptor, BidiIterator<INode> it) {
        INode n = null;
        while (it.hasNext() && (n = it.next()).getClass() == HiddenLeafNode.class)
            processHiddenNode(acceptor, (HiddenLeafNode)n);
        return n;
    }

    INode skipWhiteSpaceBackwards(IHighlightedPositionAcceptor acceptor, BidiIterator<INode> it) {
        INode n = null;
        while (it.hasPrevious() && (n = it.previous()).getClass() == HiddenLeafNode.class)
            processHiddenNode(acceptor, (HiddenLeafNode)n);
        return n;
    }

    void processHiddenNode(IHighlightedPositionAcceptor acceptor, HiddenLeafNode node) {
        if (node.getGrammarElement() instanceof TerminalRuleImpl) {
            TerminalRuleImpl ge = (TerminalRuleImpl)node.getGrammarElement();
            if (ge.getName().equalsIgnoreCase("SL_COMMENT") || ge.getName().equalsIgnoreCase("ML_COMMENT"))
                acceptor.addPosition(node.getOffset(), node.getLength(), COMMENT);
        }

    }

}
